﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebNext.Domain.Entities;
using WebNext.Domain;
using WebNext.UI.Utils;
using WebNext.UI.Utils.Validators;

namespace WebNext.UI.Models.Comments
{
	public class CommentViewModel
	{
		public int Id
		{
			get;
			set;
		}

		public int EntryId
		{
			get;
			set;
		}

		public string Author
		{
			get;
			set;
		}

		public string Content
		{
			get;
			set;
		}

		public int Rank
		{
			get;
			set;
		}

		public string CaptchaKey
		{
			get;
			set;
		}

		public string CaptchaValue
		{
			get;
			set;
		}

		public CommentDisplayMode DisplayMode
		{
			get;
			set;
		}

		public DateTime Created
		{
			get;
			set;
		}

		public bool IsAuthorValid
		{
			get;
			private set;
		}

		public bool IsContentValid
		{
			get;
			private set;
		}

		public bool IsCaptchaValid
		{
			get;
			private set;
		}

		public bool IsLogged
		{
			get;
			set;
		}

		public CommentViewModel(int id, int entryId, string author, string content)
		{
			this.Id = id;
			this.EntryId = entryId;
			this.Author = author;
			this.Content = content;

			this.IsAuthorValid = true;
			this.IsContentValid = true;
			this.IsCaptchaValid = true;

			this.DisplayMode = CommentDisplayMode.View;
		}

		public CommentViewModel(Comment comment, bool isLogged) : this(comment.Id, comment.Entry.Id, comment.Author, comment.Content)
		{
			this.Created = comment.CreatedOn;
			this.Rank = comment.Rank;
			this.IsLogged = isLogged;
		}

		public ValidationResult Validate()
		{
			ValidationResult result = new ValidationResult();
			
			if (string.IsNullOrWhiteSpace(this.Author))
			{
				result.AddError(Properties.Resources.CommentAuthorCannotBeEmpty);
				this.IsAuthorValid = false;
			}
			else if (this.Author.Length > Comment.MaxAuthorLength)
			{
				result.AddError(Properties.Resources.CommentAuthorTooLong, Comment.MaxAuthorLength);
				this.IsAuthorValid = false;
			}

			if (string.IsNullOrWhiteSpace(this.Content))
			{
				result.AddError(Properties.Resources.CommentContentCannotBeEmpty);
				this.IsContentValid = false;
			}
			else if (this.Author.Length > Comment.MaxAuthorLength)
			{
				result.AddError(Properties.Resources.CommentContentTooLong, Comment.MaxContentLength);
				this.IsContentValid = false;
			}

			if (this.Id == 0)
			{
				IValidator captchaValidator = new CaptchaValidator(this.CaptchaKey, this.CaptchaValue);
				ValidationResult captchaValidationResult = captchaValidator.Validate();
				if (!captchaValidationResult.IsValid)
				{
					result.Merge(captchaValidationResult);
					this.IsCaptchaValid = false;
				}
			}
			return result;
		}

		public void UpdateFrom(CommentViewModel sourceCommentModel)
		{
			this.Id = sourceCommentModel.Id;
			this.Author = sourceCommentModel.Author;
			this.Content = sourceCommentModel.Content;
			this.Rank = sourceCommentModel.Rank;
			this.IsAuthorValid = sourceCommentModel.IsAuthorValid;
			this.IsContentValid = sourceCommentModel.IsContentValid;
			this.IsCaptchaValid = sourceCommentModel.IsCaptchaValid;
		}
	}
}